let BASE_URL = "https://wlsy.nanjingyunpeng.cn";
// #ifdef H5
// BASE_URL = "/api";
// #endif
const request = (obj, data, loading) => {
    if (loading) uni.showLoading({ title: "加载中",mask: true});
	let token  = uni.getStorageSync("token") || ""
    let httpDefaultOpts = {
        url: BASE_URL + obj.url,
        data: data,
        beforeSend: function (xmlHttp) {
            xmlHttp.setRequestHeader("If-Modified-Since", "0");
            xmlHttp.setRequestHeader("Cache-Control", "no-cache");
        },
        method: obj.method || 'post',
        dataType: "json",
        header: {
            "Content-type": "application/json",
        },
    };
    if (token) {
        if (httpDefaultOpts.header) {
            httpDefaultOpts.header["Authorization"] = "Bearer " + token;
        } else {
            httpDefaultOpts.header = { Authorization: "Bearer " + token };
        }
    } else {
        console.log("获取不到userToken");
    }
    let promise = new Promise(function (resolve, reject) {
        uni.request(httpDefaultOpts)
            .then((res) => {
                if (loading) uni.hideLoading();
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else if (res.statusCode == 401) {
                    uni.removeStorageSync("userInfo");
                    uni.removeStorageSync("tokenInfo");
                    return uni.reLaunch({ url: "/pages/login/login" });
                } else {
                    reject(res);
                }
            })
            .catch((response) => {
                reject(response);
            });
    });
    return promise;
};
export default {
    BASE_URL,
    request,
};
